package com.sunny.l17;

public class Solution {
//    public static void main(String[] args) {
//
//        int[] gas = {1,2,3,7};
//        int[] cost = {5,2,5,1};
//        System.out.println(new Solution().canCompleteCircuit(gas, cost));
//
//    }
    public int canCompleteCircuit(int[] gas, int[] cost) {

        int start = gas.length - 1;
        int end = 0;
        int sum = gas[start] - cost[start] + gas[end] - cost[end];
        while (start != end){
            if(sum < 0){
                start--;
                sum += gas[start] - cost[start];
            }else{
                end++;
                sum += gas[end] - cost[end];
            }
        }
        if(sum < 0){
            return -1;
        }
        return start;

    }
}
